package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /*商品查询*/
    @GetMapping("/list")
    public PageDTO getItemList(@RequestParam Integer page, @RequestParam Integer size) {
        return itemService.listPage(page, size);
    }

    /*根据id查询商品*/
    @GetMapping("/{id}")
    public Item getItemById(@PathVariable("id") Long id) {
        return itemService.getItemById(id);
    }

    /*新增商品*/
    @PostMapping()
    public boolean addItem(@RequestBody Item item) {
        return itemService.addItem(item);
    }

    /*上下架商品*/
    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") long id, @PathVariable("status") int status) {
        itemService.updateItemStatus(id, status);
    }

    /*修改商品*/
    @PutMapping()
    public boolean updateItem(@RequestBody Item item) {
        return itemService.updateItem(item);
    }

    /*删除商品*/
    @DeleteMapping("/{id}")
    public boolean deleteItem(@PathVariable("id")Long id) {
        return itemService.deleteItemById(id);
    }


    @PutMapping("/stock/{itemId}/{num}")
    public void  updateStock(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.stock(itemId,num);

    }
}
